package com.shujia.wyh.day28;

import java.io.*;
import java.net.Socket;

/*
        TCP协议客户端程序编写：
            1、创建客户端的Socket对象
            2、创建BufferedReader读取本地文件
            3、获取通道中的字节输出流对象
            4、读取数据并写入到通道中
            5、释放资源
 */
public class UploadClient {
    public static void main(String[] args) throws Exception {
        //创建客户端的Socket对象
        Socket s = new Socket("192.168.10.112", 12345);

        //创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("b.txt"));

        //获取通道中的字节输出流对象
//        OutputStream os = s.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //告诉服务器你别等了，我没有数据可以过来了
        s.shutdownOutput();

        //接收服务器的反馈
        BufferedReader br2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String s1 = br2.readLine();
        System.out.println("服务器反馈：" + s1);

        //释放资源
        br.close();
        s.close();

    }
}
